# Data preperation: thetameta2.Rdata

# The data file "Norwegian Citizen Panel wave 1-14 - EN.sav" will be available at:
# https://nsd.no/nsddata/serier/norsk_medborgerpanel_eng.html
# In the mean time you can also older versions of the Norwegian Citizen Panel (e.g., wave 1 - 13),
# which is also available at: https://nsd.no/nsddata/serier/norsk_medborgerpanel_eng.html
# The data file "oil-elect-panel-processed-for-R.dta" includes a compound of cariables from the NCP:
# w01_k24, w03_r3k24, r4k24, r5km11, r9k24a, r9k24_1_1, r6k34_20, r7k34_20, r8k34_20, r9k34_20 

# clear all
rm(list=ls(all=TRUE))

# Load packages
library(foreign)

# Import data
setwd("P:/2017-pathways/new/4-model-output")
load("pathwaysPrevFit9.Rdata")
setwd("P:/2017-pathways/new/2-data")
oilworkdata <- read.dta("oil-elect-panel-processed-for-R.dta")
ncp <- read.spss(file=paste("Norwegian Citizen Panel wave 1-14 - EN.sav", sep="/"),
                 use.value.labels=FALSE,
                 to.data.frame=TRUE,
                 trim.factor.names=TRUE)

# Merge datasets
myvars<-c("responseid", 
          "w03_r3dvh_1", "r4dvh_1", "r5dvh_1", "r6dvh_1", "r7dvh_1", 
          "w03_r3km23", "r4km1", "r5km6", "r6km236", "r8bekym", "r10km2", 
          "r7km2", "r8km2", "r7P4_1" 
)

numdata<-ncp[myvars]
numdata$serial<-as.integer(numdata$responseid)

extendedmeta<-merge(meta_o_p_data, numdata, 
                    by.x="responseid",
                    by.y="responseid",
                    all.x=TRUE,
                    all.y=FALSE
)

# Prepare data for regression analysis
oilworkdata$oilwork.orig <- oilworkdata$oilwork2

theta<-as.data.frame(pathwaysPrevFit9$theta[1:2946, 1])
names(theta)[1]<-paste("theta1")
theta$theta2<-pathwaysPrevFit9$theta[1:2946, 2]
theta$theta3<-pathwaysPrevFit9$theta[1:2946, 3]
theta$theta4<-pathwaysPrevFit9$theta[1:2946, 4]
theta$theta5<-pathwaysPrevFit9$theta[1:2946, 5]
theta$theta6<-pathwaysPrevFit9$theta[1:2946, 6]
theta$theta7<-pathwaysPrevFit9$theta[1:2946, 7]
theta$theta8<-pathwaysPrevFit9$theta[1:2946, 8]
theta$theta9<-pathwaysPrevFit9$theta[1:2946, 9]
theta$id<-as.numeric(rownames(theta))

extendedmeta$id<-as.numeric(rownames(extendedmeta))
thetameta<-merge(extendedmeta, theta, 
                 by.x="id",
                 by.y="id",
                 all.x=TRUE,
                 all.y=FALSE
)

thetameta$treatment1<-0
thetameta$treatment1[thetameta$treatment==1]<-1
thetameta$treatment2<-0
thetameta$treatment2[thetameta$treatment==2]<-1
thetameta$treatment3<-0
thetameta$treatment3[thetameta$treatment==3]<-1

thetameta$worried <- thetameta$r8bekym
thetameta$worried[thetameta$r8bekym==97] <- NA
thetameta$worried <- 6-thetameta$worried
with(thetameta, 
     table(r8bekym, worried))
thetameta$lofoten <- thetameta$r7dvh_1
thetameta$lofoten[thetameta$r7dvh_1>90] <- NA
with(thetameta,
     table(r7dvh_1, lofoten, useNA = "always"))

thetameta2 <- merge(thetameta, oilworkdata, 
                    by.x="responseid",
                    by.y="responseid",
                    all.x=TRUE,
                    all.y=FALSE)

thetameta2$thetaperc2 <- thetameta2$theta2 * 100
thetameta2$thetaperc3 <- thetameta2$theta3 * 100
thetameta2$thetaperc8 <- thetameta2$theta8 * 100
thetameta2$thetaperc6 <- thetameta2$theta6 * 100

thetameta2$he[!is.na(thetameta2$r7P4_1)]<-0
thetameta2$he[thetameta2$r7P4_1==3]<-1

# Save data
setwd("P:/2017-pathways/new/2-data")
save(thetameta2, file="thetameta2.Rdata")